package com.hfi.restful.web.handler;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpResponseException;
import org.apache.http.impl.client.AbstractResponseHandler;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * 自定义restTemplate异常捕获
 * @author ChangLiang
 * @date 2019/9/6
 */
public class HfiResponseHandler extends AbstractResponseHandler<String> {
    @Override
    public String handleEntity(HttpEntity entity) throws IOException {
        return EntityUtils.toString(entity);
    }

    @Override
    public String handleResponse(
            final HttpResponse response) throws HttpResponseException, IOException {
        final StatusLine statusLine = response.getStatusLine();
        //
        final HttpEntity entity = response.getEntity();
        return entity == null ? null : handleEntity(entity);
    }
}
